package cn.imvc.app.models;

import java.util.List;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.DealerLevel;
import cn.imvc.app.entities.PriceClass;
import cn.imvc.app.repositories.DealerLevelRepository;
import cn.imvc.app.repositories.PriceClassRepository;
import cn.imvc.core.HttpContext;
import cn.imvc.data.domain.ViewRequest;

@Service("dealerLevelModel")
public class DealerLevelModelImpl implements DealerLevelModel {
	@Autowired
	private DealerLevelRepository dealerLevelRepository;
	@Autowired
	private PriceClassRepository priceClassRepository;
	@Autowired
	private RefRelationModel refRelationModel;
	public String delete(int id) {
		String result = refRelationModel.deleteDateCheck("dealer_level", "id", id);
		if(result.equals("")){
			dealerLevelRepository.delete(id);
			return "";
		}else{
			return result;
		}
	}

	public Page<DealerLevel> find(ViewRequest viewRequest) {
		return dealerLevelRepository.findAll(viewRequest);
	}

	private boolean sqlValidate(String sql) {
    	sql = sql.toLowerCase();
        String[] badStrs = {"create ","insert", "update ", "delete","drop","exec","execute","alter","declare","truncate"};
        for (int i = 0; i < badStrs.length; i++) {
            if (sql.indexOf(badStrs[i]) >= 0) {
                return true;
            }
        }
        return false;
    }
	
	
	public Boolean save(int priceClassId,DealerLevel dealerLever) {
		// 等级名字不能相同
		String name = dealerLever.getName();
		List<DealerLevel> dls = dealerLevelRepository.findByName(name);
		if (dls.size() > 0) {
			return false;
		}
		// 等级级别不能相同
		int theLevel = dealerLever.getLevel();
		dls = dealerLevelRepository.findByLevel(theLevel);
		if (dls.size() > 0) {
			return false;
		}
		dealerLever.setPriceClass(priceClassRepository.getOne(priceClassId));
		dealerLevelRepository.save(dealerLever);
		return true;
	}

	public DealerLevel getOne(int id) {
		return dealerLevelRepository.getOne(id);
	}

	public Boolean update(int priceClassId,DealerLevel dealerLever,int id) {
		DealerLevel oldDealerLevel = dealerLevelRepository.findOne(id);
		//等级名字不能相同
		String name = dealerLever.getName();
		List<DealerLevel> dls = dealerLevelRepository.findByName(name);
		if(dls.size()>0 && !name.equals(oldDealerLevel.getName())){
			return false;
		}
		//等级级别不能相同
		int theLevel = dealerLever.getLevel();
		dls = dealerLevelRepository.findByLevel(theLevel);
		if(dls.size()>0 && theLevel != oldDealerLevel.getLevel()){
			return false;
		}
		dealerLever.setPriceClass(priceClassRepository.getOne(priceClassId));
		if(dealerLever.getImgUrl().isEmpty()){
		BeanUtils.copyProperties(dealerLever, oldDealerLevel, "id","imgUrl");
		}else{
			BeanUtils.copyProperties(dealerLever, oldDealerLevel, "id");
		}
		dealerLevelRepository.saveAndFlush(oldDealerLevel);
			return true;
	}

	public List<DealerLevel> findAll() {
		return dealerLevelRepository.findAll();
	}
	
	
	/**
	 * 供选择使用
	 */
	public String treeLookup() {
		List<DealerLevel> dealerLevels = dealerLevelRepository.findAll();
		StringBuffer tree = new StringBuffer();
		tree.append(
			"<table class='table' width='100%' layoutH='138'><thead><tr>"
			+ "	<th width='120'>等级名称</th><th width='200'>等级高低</th><th width='100'>分佣等级(%)</th><th width='100'>加盟积分</th><th width='100'>加盟费用</th><th width='100'>等级图片</th><th width='100'>选择</th>"
			+ "	</tr></thead><tbody>");
		
		for (DealerLevel dealerLevel : dealerLevels) {
			tree.append("<tr>");
			tree.append("<td>"+dealerLevel.getName()+"</td>");
			tree.append("<td>"+dealerLevel.getLevel()+"</td>");
			tree.append("<td>"+dealerLevel.getPercent()+"</td>");
			tree.append("<td>"+dealerLevel.getJoinPoint()+"</td>");
			tree.append("<td>"+dealerLevel.getJoinMoney()+"</td>");
			tree.append("<td><img width=\"50px\" src=\""+HttpContext.getContext().getAttribute("rootUrl").toString()+"/../"+dealerLevel.getImgUrl()+"\"</img></td>");
			tree.append("<td><a href=\"javascript:\" onclick=\"$.bringBack({code:'"+dealerLevel.getId()+"', name:'"+dealerLevel.getName()+"'})\">"
					+"<font color=\"red\">选择</a></td>"
					+ "</tr>");
		}
		tree.append("</tbody></table>");
		return tree.toString();
	}
	
	/**
	 * 查询价格等级，0为可用，1为不可用
	 */
	public List<PriceClass> getPriceClass() {
		return priceClassRepository.findByIsDisabledOrderBySort((byte)0);
	}

	@Override
	public List<DealerLevel> findByLevelGreaterThan(int level) {
		return dealerLevelRepository.findByLevelGreaterThanOrderByLevelAsc(level) ;
	}

	@Override
	public List<DealerLevel> findByLevel(int level) {
		return dealerLevelRepository.findByLevel(level);
	}
}
